﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件功能描述：图上上传Ajax请求服务端处理程序。
  //创建人：赵国伟
  //创建时间：2012-03-18 12:34:54
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

using Shopping.Common;
using Shopping.BLL;
using Shopping.Model;

public partial class Manage_AS_AjaxUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string action = Request.QueryString["Action"] + "";
            switch (action)
            {
                case "UploadFiles":  //处理文件的上传； by 赵国伟 2012-03-18
                    UploadFiles();
                    break;
            }

        }
        Response.End();
    }


    #region "处理文件的上传 by 赵国伟 2012-03-18"
    /// <summary>
    /// 处理图片的上传
    /// </summary>
    public void UploadFiles()
    {
        HttpPostedFile uploadfile = Request.Files["Filedata"];
        string dateStr = DateTime.Now.ToString("yyyy-MM-dd");
        //先将文件保存于临时目录中；
        string path = "";
        if(Config.ShoppingFolder.EndsWith("\\"))
            path = Config.ShoppingFolder + "tmp\\" + dateStr;
        else
            path = Config.ShoppingFolder + "\\tmp\\" + dateStr;

        //判断下文件夹是否存在； 不存在创建
        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);
        string extName = Path.GetExtension(uploadfile.FileName);  //获得扩展名；
        string fileName = Guid.NewGuid() + extName;
        string fullName = path + "\\" + fileName;
        uploadfile.SaveAs(fullName);  //将文件保存到磁盘中；
        //记录下盖文件的访问地址；
        string fileUrl = "";
        if(Config.ShoppingFolderUrl.EndsWith("/"))
            fileUrl = Config.ShoppingFolderUrl + "tmp/" + dateStr + "/" + fileName;
        else
            fileUrl = Config.ShoppingFolderUrl + "/tmp/" + dateStr + "/" + fileName;

        Response.Write(fileUrl);
    }
    #endregion



}